/**!
 * 企业政策库
 * author: wangkai;
 */
$(function() {
    var $baseinfo = $("#baseinfo"),
        $declarationSubject = $("#declarationSubject"),
        $declarationCondition = $("#declarationCondition"),
        $declarationProcedure = $("#declarationProcedure"),
        $materialinfo = $("#materialinfo"),
        $commonProblem = $("#commonProblem"),
        $consultlist = $("#consultlist");

    var baseinfoTmpl = Util.clearHtml($("#baseinfo-tmpl").html()),
        declarationSubjectTmpl = Util.clearHtml($("#declarationSubject-tmpl").html()),
        declarationConditionTmpl = Util.clearHtml($("#declarationCondition-tmpl").html()),
        declarationProcedureTmpl = Util.clearHtml($("#declarationProcedure-tmpl").html()),
        materialinfoTmpl = Util.clearHtml($("#materialinfo-tmpl").html()),
        commonProblemTmpl = Util.clearHtml($("#commonProblem-tmpl").html()),
        consultlistTmpl = Util.clearHtml($("#consultlist-tmpl").html());

    var M = Mustache;
    M.parse(baseinfoTmpl);
    M.parse(declarationSubjectTmpl);
    M.parse(declarationConditionTmpl);
    M.parse(declarationProcedureTmpl);
    M.parse(materialinfoTmpl);
    M.parse(commonProblemTmpl);
    M.parse(consultlistTmpl);
    var PolicyR = {
            DateLimit: false,
            CertificateStatus: false,
            PolicyGuid: Util.getUrlParams("policyGuid"), //获取 政策唯一ID
            UserGuid: $.cookie('the_userid'), //用户唯一标识
            LimitDesc: ""
        }
        /*政策信息*/
    $.ajax({
        url: Util.AjaxRUrl + 'getPolicyBasicInfo',
        type: "post",
        dataType: "json",
        data: {
            param: '{"policyGuid":"' + PolicyR.PolicyGuid + '"}'
        },
        success: function(data) {
            renderData(data);
        },
        error: function(XMLHttpRequest, txtStatus, errorThrown) {
            //console.log(XMLHttpRequest);
        }
    });
    //政策详情渲染
    function renderData(data) {
        if (data) {
            if (data.PolicyBasicInfo) {
                $(".ewb-info-tt").text(data.PolicyBasicInfo.policyName);
                //基本信息模块
				var sssend = new Date(data.PolicyBasicInfo.declareEndDate);
				var datet2 = sssend.setDate(sssend.getDate()+1);
				datet2 = new Date(datet2);
				if(datet2.getMonth()<10){
					data.PolicyBasicInfo.declareEndDate = datet2.getFullYear()+"-"+(datet2.getMonth()+1)+"-"+datet2.getDate();
				}else{
					data.PolicyBasicInfo.declareEndDate = datet2.getFullYear()+"-"+(datet2.getMonth()+1)+"-"+datet2.getDate();
				}
				
				
                $baseinfo.html(M.render(baseinfoTmpl, data.PolicyBasicInfo));
                //政策级别过滤：区级提供互动：咨询、申报
                if (data.PolicyBasicInfo.policyLevel == "区级") {
                    $(".ewb-info-links").show();
                }
                //时限判断：
                if (data.PolicyBasicInfo.declareStartDate && data.PolicyBasicInfo.declareEndDate) {
                    var datef = Util.stringToDate(data.PolicyBasicInfo.declareStartDate);
					
					var dateend = Util.stringToDate(data.PolicyBasicInfo.declareEndDate);
					//var datet = dateend.setDate(dateend.getDate()+1);
					//datet = new Date(datet);
					
                    //var datet = Util.stringToDate(endate);
                    var tnow = new Date();
                    //tnow = new Date(tnow.getFullYear(), tnow.getMonth(), tnow.getDate()); //待完善
                    if (datef > tnow) {
                        PolicyR.LimitDesc = "申报未开始";
                        //PolicyR.DateLimit=false;
                    } else if (dateend <= tnow) {
                        PolicyR.LimitDesc = "申报已截止";
                        //PolicyR.DateLimit=false;
                    } else {
                        PolicyR.LimitDesc = "申报中";
                        PolicyR.DateLimit = true;
                    }
                    $(".ewb-info-remind").text(PolicyR.LimitDesc)
                }
                //申报主体模块
                $declarationSubject.html(M.render(declarationSubjectTmpl, data.PolicyBasicInfo));
                //申报条件模块
                $declarationCondition.html(M.render(declarationConditionTmpl, data.PolicyBasicInfo));
                //申报、审核程序模块
                $declarationProcedure.html(M.render(declarationProcedureTmpl, data.PolicyBasicInfo));
            }
            if (data.MaterialsInfos && data.MaterialsInfos.length) {
                //申报材料模块
                $materialinfo.html(M.render(materialinfoTmpl, { items: data.MaterialsInfos }));
                $.each(data.MaterialsInfos, function(index, val) { //加入序号
                    var $lfile = $materialinfo.find(".ewb-baseinfo[data-furl]").eq(index);
                    if ($lfile.data("furl") == "" || $lfile.data("furl") == undefined) {
                        $lfile.removeClass('fileopr').html("<p>暂无</p>");
                    } else {
                        $lfile.click(function(event) {
                            var furl = $(this).data("furl");
                            if (furl != "") {
                                furl = Util.FileRUrl + furl;
                                window.open(furl, "_blank");
                            } else {
                                alert("程序异常");
                            }
                        });
                    }
                    index++;
                    $materialinfo.find(".ewb-itemnumber").eq(index - 1).append('<span>' + index + "</span>");
                });
            }
            if (data.ProblemsInfo && data.ProblemsInfo.length) {
                //常见问题模块
                $commonProblem.html(M.render(commonProblemTmpl, { items: data.ProblemsInfo }));
                $("#commonProblem").find("li").css("marginTop", "10px");
                $("#commonProblem").find("li").eq(0).addClass("mt0");
            }
        }
        // TAB切换,依赖于tabview.js组件
        $(".tab-view").each(function(index, el) {
            new TabView({
                dom: el,
                // 触发tab切换的事件：click|mouseover
                triggerEvent: 'click',
                activeCls: 'current'
            });
        });
    }
    //热门咨询请求
    $.ajax({
        url: Util.AjaxRUrl + 'GetHotAdvice',
        type: "post",
        dataType: "json",
        data: {
            param: '{policyGuid:"' + PolicyR.PolicyGuid + '"}'
        },
        success: function(data) {
            HotAdviceData(data);
        },
        error: function(XMLHttpRequest, txtStatus, errorThrown) {
            //console.log(XMLHttpRequest);
        }
    });
    //热门咨询渲染
    function HotAdviceData(data) {
        if (data.getHotAdvice && data.getHotAdvice.length) {
            $(".hot-consulting").show();
            $consultlist.html(M.render(consultlistTmpl, { items: data.getHotAdvice }));
            $("#consultlist").find("li").eq(0).addClass("mt0");
        }
    }
    //实名认证
    function certificateConfirm(uguid) {
        var result = false;
        $.ajax({
            url: Util.AjaxRUrl + 'certificateStatus',
            type: "post",
            dataType: "json",
            async: false,
            data: {
                param: '{"userGuid":"' + uguid + '"}'
            },
            success: function(data) {
                if (data) {
                    if (data.returnInfo) {
                        var status = data.returnInfo.status;
                        if (status.indexOf("通过") > -1) {
                            PolicyR.CertificateStatus = true;
                        } else {
                            PolicyR.CertificateStatus = false;
                            alert(status); //失败提示
                        }
                    }
                }
                result = PolicyR.CertificateStatus;
            },
            error: function(XMLHttpRequest, txtStatus, errorThrown) {
                //console.log(XMLHttpRequest);
            }
        });
        return result;
    }
    //网上申报
    $("#online-reporting").on('click', function() {
        if (PolicyR.DateLimit) {
            if (PolicyR.UserGuid != '' && PolicyR.UserGuid != null) {
                //实名认证
                if (!PolicyR.CertificateStatus) {
                    certificateConfirm(PolicyR.UserGuid); //未认证时执行认证
                }
                if (PolicyR.CertificateStatus) {
					$.ajax({
						url: Util.AjaxRUrl + 'checkApplyAgain',
						type: "post",
						dataType: "json",
						async: false,
						data: {
							param: '{"userGuid":"'+PolicyR.UserGuid+'","policyGuid":"'+PolicyR.PolicyGuid+'"}'
						},
						success: function(data) {
							//console.log(data);
							if(data.returnInfo.isok=="1"){
								window.location.href = "/policyDeclaration.html?policyGuid=" + PolicyR.PolicyGuid;
							}else{
								alert("该政策已申报!");
							}
						},
						error: function(XMLHttpRequest, txtStatus, errorThrown) {
							//console.log(XMLHttpRequest);
						}
					});
  
                }
            } else {
                alert("请先登录");
            }
        } else {
            alert(PolicyR.LimitDesc);
        }
    });
    //咨询弹窗
    $('.ewb-consult-btn').on('click', function() {
        if (PolicyR.DateLimit) {
            if (PolicyR.UserGuid != '' && PolicyR.UserGuid != null) {
                //实名认证
                if (!PolicyR.CertificateStatus) {
                    certificateConfirm(PolicyR.UserGuid); //未认证时执行认证
                }
                if (PolicyR.CertificateStatus) {
                    parent.layer.open({
                        type: 2,
                        area: ['750px', '421px'],
                        shadeClose: false,
                        content: ['resultIframe.html?policyGuid=' + PolicyR.PolicyGuid, 'no'], //讲政策唯一ID通过地址栏传入弹出框页面
                    });
                }
            } else {
                alert("请先登录");
            }
        } else {
            alert(PolicyR.LimitDesc);
        }
    });
    //收藏
    $("#collectionbtn").on('click', function() {
        if (PolicyR.DateLimit) {
            if (PolicyR.UserGuid != '' && PolicyR.UserGuid != null) {
                //实名认证
                if (!PolicyR.CertificateStatus) {
                    certificateConfirm(PolicyR.UserGuid); //未认证时执行认证
                }
                if (PolicyR.CertificateStatus) {
                    $.ajax({
                        url: Util.AjaxRUrl + 'policyCollect',
                        type: "post",
                        dataType: "json",
                        data: {
                            param: '{"policyGuid":"' + PolicyR.PolicyGuid + '","userGuid":"' + PolicyR.UserGuid + '"}'
                        },
                        success: function(data) {
                            collectionData(data);
                        },
                        error: function(XMLHttpRequest, txtStatus, errorThrown) {
                            //console.log(XMLHttpRequest);
                        }
                    });
                }

            } else {
                alert("请先登录");
            }
        } else {
            alert(PolicyR.LimitDesc);
        }

        function collectionData(data) {
            if (data) {
                if (data.returnInfo) {
                    var isok = data.returnInfo.isok;
                    switch (isok) {
                        case 1:
                            alert("收藏成功");
                            break;
                        case 2:
                            alert("收藏失败");
                            break;
                        case 3:
                            alert("已经收藏");
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    });

});

function lookFile(selector) {
    var furl = $(selector).data("furl");
    if (furl != "") {
        furl = Util.FileRUrl + furl;
        window.open(furl, "_blank");
    } else {
        alert("暂无对应模板");
    }
}
